<template>
	<!-- 当数据还没有请求完成的时候，页面渲染没有数据会导致页面闪烁 -->
	<view v-if="detailList.goods_name" class="detail-container">
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,i) in detailList.pics" :key="i">
				<!-- 为每一张图片添加点击事件，可以放大预览 -->
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="detail-box">
			<!-- 商品价格 -->
			<view class="price">￥{{detailList.goods_price}}</view>
			<!-- 商品主题 -->
			<view class="detail-body">
				<!-- 商品名字 -->
				<view class="detail-name">{{detailList.goods_name}}</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="heart" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>

			</view>
			<!-- 运费信息 -->
			<view class="yf">快递:免运费</view>
		</view>
		<!-- 商品详细区域 -->
		<rich-text :nodes="detailList.goods_introduce"></rich-text>
		<!-- 商品导航组件区域 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	//从store的vuex中导入mapstate，mapMutations，mapGetters方法
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				//商品详情对象
				detailList: {},
				//商品的导航组件
				options: [
					// 	{
					// 	icon:'headphones',
					// 	text:'客服'
					// },
					{
						icon: 'shop',
						text: '店铺',
						// info:2,
						infoBackgroundColor: '#007aff',
						infoColor: "red"
					},
					{
						icon: 'cart',
						text: '购物车',
						info: 0
					}
				],
				//购买和购物车
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立刻购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			//获取商品的ID
			const goods_id = options.goods_id
			//调用请求商品详情的方法
			this.getdetailList(goods_id)
		},
		methods: {
			//调用请求商品详情的方法
			async getdetailList(id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: id
				})
				console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()

				//获取数据后。因为goods_introduce的图片下方有空隙，所以要实现样式替换replace字符串替换，还需要将.webp的图片改为.jpg图片
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;"').replace(/.webp/g, '.jpg')
				this.detailList = res.message
			},
			//实现轮播图放大预览的效果
			preview(i) {
				//调用uni.previewImage()方法预览图片
				uni.previewImage({
					//预览时默认图片的索引
					current: i,
					//所有图片的URL地址
					urls: this.detailList.pics.map((x) => x.pics_big)
				})
			},
			// goods_nav点击事件
			onClick(e) {
				if (e.content.text === '购物车')
					uni.switchTab({
						url: '/pages/cart/cart'
					})
			},
			//goods_nav按钮的点击事件
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					//组织商品的信息对象
					const goods = {
						goods_id: this.detailList.goods_id,
						goods_name: this.detailList.goods_name,
						goods_price: this.detailList.goods_price,
						goods_count: 1,
						goods_small_logo: this.detailList.goods_small_logo,
						goods_state: true
					}
					// 再调用mutations中的addToCart方法
					this.addToCart(goods)
				}
			},
			//调用vuex中的Mutations方法
			...mapMutations('m_cart', ['addToCart'])
		},
		//将cart展开到计算属性中
		computed: {
			...mapState('m_cart', []),
			...mapGetters('m_cart', ['total'])
		},
		//监听器监听vuex传来的total计算属性
		watch: {
			//total监听对象
			total: {
				//handler用于定义侦听器的function处理函数
				handler(newVal) {
					//findResult 获得的是购物车这个属性的值
					const findResult = this.options.find(x => x.text === '购物车')
					if (findResult) {
						findResult.info = newVal
					}
				},
				//immediate属性控制是否在页面首次加载完毕后调用
				immediate:true
			}
		}
	}
</script>

<style lang="scss">
	.detail-container {
		padding-bottom: 50px;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.detail-box {
		display: flex;
		flex-direction: column;
		padding: 10px;

		.price {
			font-size: 20px;
			color: #c00000;
		}

		.detail-body {
			display: flex;
			flex-wrap: nowrap;

			.detail-name {
				margin: 10px;
				margin-left: 0;
				font-size: 14px;
				border-right: 1px solid #efefef;
			}

			.favi {
				display: flex;
				flex-direction: column;
				justify-content: center;
				width: 100px;
				text-align: center;
				font-size: 14px;
				color: gray;
			}
		}

		.yf {
			font-size: 14px;
			color: gray;
		}
	}

	.goods_nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
